import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/resource/resources.dart';

enum GTToolTipPositionV3 {
  topCenter,
  topStart,
  topEnd,
  startTop,
  startCenter,
  startBottom,
  bottomStart,
  bottomCenter,
  bottomEnd,
  endTop,
  endCenter,
  endBottom
}

class GTTooltipWidgetV3 extends StatefulWidget {
  final String text;
  final double tooltipMaxWidth;
  final GTToolTipPositionV3 position;
  final double targetWidth;
  final bool? showClose;
  final VoidCallback? onCloseClick;

  const GTTooltipWidgetV3({
    super.key,
    required this.text,
    this.tooltipMaxWidth = 250,
    required this.position,
    required this.targetWidth,
    this.showClose = false,
    this.onCloseClick,
  });

  @override
  State<StatefulWidget> createState() => _TooltipStateV3();
}

class _TooltipStateV3 extends State<GTTooltipWidgetV3> {
  bool isRTL = GTThemeProvider.instance.isRTL();
  @override
  Widget build(BuildContext context) {
    // double horizontalPadding = widget.targetWidth / 2 - 18 < 0 ? 0 : widget.targetWidth / 2 - 18;
    CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center;
    switch (widget.position) {
      case GTToolTipPositionV3.topCenter:
      case GTToolTipPositionV3.bottomCenter:
      case GTToolTipPositionV3.startCenter:
      case GTToolTipPositionV3.endCenter:
        crossAxisAlignment = CrossAxisAlignment.center;
        break;
      case GTToolTipPositionV3.topStart:
      case GTToolTipPositionV3.bottomStart:
      case GTToolTipPositionV3.startTop:
      case GTToolTipPositionV3.endTop:
        crossAxisAlignment = CrossAxisAlignment.start;
        break;
      case GTToolTipPositionV3.topEnd:
      case GTToolTipPositionV3.bottomEnd:
      case GTToolTipPositionV3.startBottom:
      case GTToolTipPositionV3.endBottom:
        crossAxisAlignment = CrossAxisAlignment.end;
        break;

    }

    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: crossAxisAlignment,
      children: [
        if (widget.position == GTToolTipPositionV3.bottomCenter ||
            widget.position == GTToolTipPositionV3.bottomStart ||
            widget.position == GTToolTipPositionV3.bottomEnd) ...[
          const SizedBox(
            height: 4,
          ),
          Image.asset(
            Images.icToolTipArrowUp,
            width: 36,
            height: 8,
          ),
        ],
        Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            if (widget.position == GTToolTipPositionV3.endCenter ||
                widget.position == GTToolTipPositionV3.endTop ||
                widget.position == GTToolTipPositionV3.endBottom) ...[
              const SizedBox(
                width: 4,
              ),
             _arrowImage(true),
            ],
            _container(),
            if (widget.position == GTToolTipPositionV3.startCenter ||
                widget.position == GTToolTipPositionV3.startTop ||
                widget.position == GTToolTipPositionV3.startBottom) ...[
              _arrowImage(false),
              const SizedBox(
                width: 4,
              )
            ],
          ],
        ),
        if (widget.position == GTToolTipPositionV3.topCenter ||
            widget.position == GTToolTipPositionV3.topStart ||
            widget.position == GTToolTipPositionV3.topEnd) ...[
          Image.asset(
            Images.icToolTipArrowDown,
            width: 36,
            height: 8,
          ),
          const SizedBox(
            height: 4,
          )
        ],
      ],
    );
  }

  Widget _arrowImage(bool isLeft){
    if(isLeft){
      if(isRTL){
        return Image.asset(
          Images.icToolTipArrowRight,
          width: 8,
          height: 28,
        );
      }else{
        return Image.asset(
          Images.icToolTipArrowLeft,
          width: 8,
          height: 28,
        );
      }
    }else{
      if(isRTL){
        return Image.asset(
          Images.icToolTipArrowLeft,
          width: 8,
          height: 28,
        );
      }else{
        return Image.asset(
          Images.icToolTipArrowRight,
          width: 8,
          height: 28,
        );
      }
    }
  }

  Widget _container() {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 12),
      decoration: BoxDecoration(color: GTColorsV3.cmpt6.color, borderRadius: BorderRadius.circular(6)),
      constraints: BoxConstraints(
        maxWidth: widget.tooltipMaxWidth,
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Flexible(
            child: Text(
              widget.text,
              style: gtTextStyleW500(fontSize: 12, color: GTColorsV3.text6.color),
              softWrap: true,
              overflow: TextOverflow.clip,
            ),
          ),
          GestureDetector(
            onTap: () {
              if (widget.onCloseClick != null) {
                widget.onCloseClick!();
              }
            },
            child: Visibility(
                visible: widget.showClose ?? false,
                child: Row(
                  children: [
                    const SizedBox(
                      width: 4,
                    ),
                    Padding(
                        padding: const EdgeInsets.only(top: 3),
                        child: Icon(
                          GTIconFont.e95d,
                          color: GTColorsV3.icon6.color,
                          size: 12,
                        )),
                  ],
                )),
          )
        ],
      ),
    );
  }
}
